<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
<style>
      * {
            padding: 0;
            margin: 0;
        }

        .login {
            width: 400px;
            height: 500px;
            margin: 50px auto;
            background-color: #ecefff;
        }

        .login .head {
            height: 150px;
            width: 200px;
            line-height: 150px;
            margin: 0 auto;
        }

        .login .head span {
            font-size: 30px;
            font-weight: 700;
            margin-left: 5px;
        }

        .login .head img {
            width: 50px;
            height: 46px;
            vertical-align: middle;
        }

        .login .reg {
            height: 350px;
            width: 350px;
            margin: 0 auto;
            display: flex;
            flex-direction: column;
            background-color: white;
            border-radius: 20px;
            text-align: center;
            padding: 20px;
        }

        .login .reg .title {
            font-weight: 700;
        }

        .login .reg input {
            height: 40px;
            padding-left: 20px;
            outline: none;
            border: 0;
            background-color: #f7f7f7;
            margin: 20px;
            color: rgb(164, 163, 163);
            border-radius: 10px;
        }

        .login .reg .but {
            background-color: #5075ff;
            color: white;
            font-size: 16px;
        }
</style>
</head>
<body>
    <div class="login">
        <div class="head">
            <img src="ali.gif" alt="">
            <span>阿里云盘</span>
        </div>
        <div class="reg">
            <div class="title">注册新账号</div>
            <input type="text" placeholder="请输入账号">
            <input type="password" placeholder="请输入密码">
            <input type="password" placeholder="确认密码">
            <input type="button" class="but" value="注册">
        </div>
    </div>
</body>
</html>
<!-- 引入封装的ajax方法 -->
<script src="promise封装ajax.js"></script>

<script>
    //获取输入框标签
    var inputs = document.querySelectorAll('input')
    //给按钮绑定点击事件
    inputs[3].onclick = function () {
        //表单验证（为空校验，格式校验）
        if (inputs[0].value && inputs[1].value && inputs[2]) {
            if (/^\w$/.test(inputs[0].value) && /^\d{4}$/.test(inputs[1].value)) {
                if ((/^\d{4}$/.test(inputs[1].value) && /^\d{4}$/.test(inputs[2].value)) && (inputs[1].value == inputs[2].value)) {
                    ajax('get', 'https://yantianfeng.com/api/register', { username: inputs[0].value, password: inputs[1].value }, {
                        username: inputs[0].value,
                        password: inputs[1].value,
                    }).then((data)=>{
                        if (data.code == 200) {
                        alert('注册成功')
                    } else {
                        alert('注册失败')
                    }
                    })
                }

            } else {
                alert('请正确输入账号/密码')
            }

        } else {
            alert('账号/密码为空，请输入内容')
        }

    }
    </script>